﻿@model int?
<script>
    $(document).ready(function () {
        var t = $('#UserList').dataTable({
            "serverSide": true,
            "ajax": {
                "url": "@Url.Action("GetUserPaged")",
                "type": "POST", data: {
                    includeRole:false
                }
            },
            select: { style: 'multi', selector: 'td:first-child' },
            "columnDefs": [{ sDefaultContent: '', aTargets: ['_all'] }],
            columns: [
                { title: '<label class="checkbox i-checks"><input type="checkbox" /><i></i></label>', data: null, sortable: false, width: 30, "sClass": 'checkColumn select-checkbox' },
                { title: '账号', data: 'userName', width: 70, "sClass": "center" },
                { title: "姓名", data: 'person.fullName', width: 160, "sClass": "left" },
                {
                    title: "性别", data: 'person.genderCode', render: function (data, type, row, meta) {
                        var genderCode;
                        if (row.person) genderCode = row.person.genderCode;
                        else genderCode = "";
                        return app.code2Text("Gender", genderCode);
                    },
                    "sClass": "left"
                }
            ]
        });
    });


    //保存关联
    function SaveItem(url) {
        var roleId = @Model;
        
        var ids = new Array();

        var list = $("#UserList").DataTable().rows({ selected: true }).data();
        $.each(list, function (index, item) {
            ids.push(item.id);
        });
        if (ids.length === 0) {
            window.app.message("请选中一项。");
            return;
        }


        $.ajax({
            type: "POST",
            url: url,
            data: { "roleId": roleId, "userIds": ids },
            dataType: "json",
            success: function (data) {
                $("#ajaxModal .modal").modal("toggle");
                if (data.errors.length > 0) {
                    swal("出错啦！", data.errors, "error");
                } else {
                    swal("已经添加", "你已经添加成功！", "success");
                    $('#selectedUsers').DataTable().ajax.reload();
                }
            }
        });
    }

</script>
<div class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">选取用户</h4>
            </div>
            <div class="panel modal-body">
                <div class="pull-right text-black" id="SearchInput">
                    <form class=" input-group input-group-sm">
                        
                        <input type="text" name="FullName" autocomplete="off" class="form-control pull-right" placeholder="输入姓名查询">
                        <div class="input-group-btn">
                            <button type ="button" class="btn btn-default" onclick="window.app.search(this);"><i class="fa fa-search"></i></button> 
                        </div>
                    </form>
                </div>
                <div class="dataTable-tools  btn-group">
                    @Html.Bootstrap().LinkButton("确定", ToolBarIco.Save).Style(BootstrapStyle.Primary).Click("SaveItem('/Role/Relation');")
                    @Html.Bootstrap().LinkButton("关闭", ToolBarIco.Close).Style(BootstrapStyle.Default).Attribute("data-dismiss", "modal")
                </div>

                <table id="UserList" class="table table-bordered table-striped" width="100%"></table>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>